$(document).ready(function(){
    $("#errorModal").dialog({
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        width:'400px',
        maxHeight:'600px',
        title:'Input Error',
        buttons: {
            Ok: function(){
                $(this).dialog( "close" );
            }
        }
    })

    $("#btn_ok").on("click",function(){
        var bucket = $("#bucket").val();
        var key = $("#prefix").val();
        if(bucket == "" || key == ""){
            var msg = "Bucket and prefix values are required. Please, check it and try it again.";
            $("#errorModal").find(".msg").html(msg);
            $("#errorModal").dialog("open");
            return false;
        }

        var params = {
            inputBucket:bucket,
            inputKey:key,
            outputBucket:bucket,
            outputKey:key,
            conversionScope:'isfolder',
            conversionType:'headersonly'
        };
        $("#btn_ok").addClass("hide");
        var op = "<img id='img_loader' src='../assets/img/loader.gif'/>";
        $("#input_container").append(op);

        $.ajax({
           type: "POST",
           url: "/spssconverter/preprocess.do",
           data: params
         }).done(function(data) {
            if(data=="inputerror"){
                var msg = "There is no data for the provided input. Please check it and try it again.";
                $("#errorModal").find(".msg").html(msg);
                $("#errorModal").dialog("open");
                $("#img_loader").remove();
                $("#btn_ok").removeClass("hide");
            }else{
                $("#img_loader").remove();
                var op = "<button id='btn_next' class='btn_light_blue'>Next</button>";
                $("#input_container").append(op);
            }

         });
    });

    $("#input_container").on("click","button#btn_next",function(){
        window.location.href="/questions/fileselection.do";
    });
});